Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
merge-options
Advanced tools
The merge-options npm package is a utility for deeply merging multiple objects into one. It is particularly useful for configurations and settings where default options need to be overridden by user inputs or other sources.
Deep Merging of Objects
This feature allows for the deep merging of objects. Properties from the second object will override those in the first object without replacing the entire structure, allowing for fine-grained updates.
const mergeOptions = require('merge-options');
const defaultOptions = { a: 1, b: { c: 2, d: 3 } };
const userOptions = { b: { c: 4 } };
const finalOptions = mergeOptions(defaultOptions, userOptions);
console.log(finalOptions); // Output: { a: 1, b: { c: 4, d: 3 } }
Merging Arrays and Functions
merge-options can also handle merging arrays and functions. By default, it replaces the original array or function entirely with the new one.
const mergeOptions = require('merge-options');
const opts1 = { arr: [1, 2], func: function() { return 'A'; } };
const opts2 = { arr: [3, 4], func: function() { return 'B'; } };
const result = mergeOptions(opts1, opts2);
console.log(result); // Output: { arr: [3, 4], func: [Function: func] }
lodash.merge is a method from the Lodash library that provides a similar deep merging functionality. Unlike merge-options, lodash.merge is part of a larger utility library, which might be preferable for projects already using Lodash for other purposes.
deepmerge is another npm package that exclusively focuses on merging objects deeply. It offers more customization options compared to merge-options, such as array concatenation and custom merging functions, which can be useful for more complex merging scenarios.
Merge Option Objects
merge-options
considers plain objects as Option Objects, everything else as Option Values.
$ npm install --save merge-options
const mergeOptions = require('merge-options');
mergeOptions({foo: 0}, {bar: 1}, {baz: 2}, {bar: 3})
//=> {foo: 0, bar: 3, baz: 2}
mergeOptions({nested: {unicorns: 'none'}}, {nested: {unicorns: 'many'}})
//=> {nested: {unicorns: 'many'}}
mergeOptions({[Symbol.for('key')]: 0}, {[Symbol.for('key')]: 42})
//=> {Symbol(key): 42}
const mergeOptions = require('merge-options').bind({ignoreUndefined: true});
mergeOptions({foo: 'bar'}, {foo: undefined})
//=> {foo: 'bar'}
mergeOptions
recursively merges one or more Option Objects into a new one and returns that. The options
are merged in order, thus Option Values of additional options
take precedence over previous ones.
The merging does not alter the passed option
arguments, taking roughly the following steps:
const defaultOpts = {
fn: () => false, // functions are Option Values
promise: Promise.reject(new Error()), // all non-plain objects are Option Values
array: ['foo'], // arrays are Option Values
nested: {unicorns: 'none'} // {…} is plain, therefore an Option Object
};
const opts = {
fn: () => true, // [1]
promise: Promise.resolve('bar'), // [2]
array: ['baz'], // [3]
nested: {unicorns: 'many'} // [4]
};
mergeOptions(defaultOpts, opts)
//=>
{
fn: [Function], // === [1]
promise: Promise { 'bar' }, // === [2]
array: ['baz'], // !== [3] (arrays are cloned)
nested: {unicorns: 'many'} // !== [4] (Option Objects are cloned)
}
Type: object
Type: boolean
Default: false
Concatenate arrays:
mergeOptions({src: ['src/**']}, {src: ['test/**']})
//=> {src: ['test/**']}
// Via call
mergeOptions.call({concatArrays: true}, {src: ['src/**']}, {src: ['test/**']})
//=> {src: ['src/**', 'test/**']}
// Via apply
mergeOptions.apply({concatArrays: true}, [{src: ['src/**']}, {src: ['test/**']}])
//=> {src: ['src/**', 'test/**']}
Type: boolean
Default: false
Ignore undefined values:
mergeOptions({foo: 'bar'}, {foo: undefined})
//=> {foo: undefined}
// Via call
mergeOptions.call({ignoreUndefined: true}, {foo: 'bar'}, {foo: undefined})
//=> {foo: 'bar'}
// Via apply
mergeOptions.apply({ignoreUndefined: true}, [{foo: 'bar'}, {foo: undefined}])
//=> {foo: 'bar'}
MIT © Michael Mayer
FAQs
Merge Option Objects
The npm package merge-options receives a total of 1,252,655 weekly downloads. As such, merge-options popularity was classified as popular.
We found that merge-options demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.